<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
 */
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );

class NvModelRegapikey extends JModel
{
    var $enkrip='';

    function getStore(){

	   $post	= JRequest::get('post');

       // defined location url server
       $config_params =& JComponentHelper::getParams( 'com_nv' );
       $url_server = $config_params->get('url_server_api');

       // catch user input and make md5 after it combined
       $field_nama = $post[nama];
       $field_url = $post[url];
       $field_key = $post[field_key];
       $date = date('d M Y H:i:s');
       $data = $field_nama.$field_key.$field_url.$date;
       $this->enkrip = md5($data);
       $enkripsi = array("apikey"=>$this->enkrip);
	   $tuju = array("tujuan"=>"apikey");

        $dataSent = array_merge($post, $enkripsi,$tuju);


        // mengecek apakah url yang diinputkan oleh user exists atau tidak
        $urlInput = "http://".$field_url;
        $ch2 = curl_init($urlInput);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch2);
        $info = curl_getinfo($ch2);
        $httpCodeSite = $info['http_code'];
        curl_close($ch2);

        if ($httpCodeSite=='200'||$httpCodeSite=='302'){
          //transfer File using CURL
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url_server);
          curl_setopt($ch, CURLOPT_POST, true);
		  curl_setopt($ch,CURLOPT_POSTFIELDS,$dataSent);
          //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         
          curl_exec($ch);
          $info = curl_getinfo($ch);
          $httpCodeTransfer = $info['http_code'];
          curl_close($ch);
                if($httpCodeTransfer=='200'){
                     $out = "Registration Success."."<br />"."Your API KEY IS : "."<br />"."<strong>".$this->enkrip."<strong>";
                     return $out;
                }// end of if transfer succes
                else {
                    $out = "Registration Is Not Success. Your API KEY can not be generated.";
                    return $out;
                }
        }// end of if site Inputed by user not exist
        else {
            $out = "Your URL is Not Exist. Registration Is Not Success. Your API KEY can not be generated.";
            return $out;
        }



    }// end of store function

}